package com.it.common.utils

import android.content.Context
import android.content.Context.AUDIO_SERVICE
import android.media.AudioManager
import android.media.MediaPlayer
import android.util.Log

object MediaPlayerUtils {
    private const val TAG = "MediaPlayerUtils"

    var mMediaPlayer: MediaPlayer? = null
    private val mAudioManager: AudioManager? = null
    var SOUND_TIPS_PATH: String = "ringout.wav"

    var SOUND_RINGING_PATH: String = "ringing.wav"


    //    public static String SOUND_RINGING2_PATH = "ringing2.wav";
    var SOUND_RINGTONE_PATH: String = "ringtone.wav"


    var CALL_TIME_OUT: String = "timeout.mp3"


    var CALL_FAILUER: String = "call_failuer.mp3"

    var CALL_HANDUP: String = "handup.mp3"


    var OTHER_CANCEL: String = "other_cancel.mp3"


    var HANDHUP_FAILUER: String = "handhup_failuer.mp3"
    var ACCEPT_FAILUER: String = "accept_failuer.mp3"
    var CANCEL_FAILUER: String = "cancel_failuer.mp3"

    var CALL_KNOW: String = "know.wav"

    /**
     * 是否免提
     * @param flag Boolean
     * @param context Context
     */
    fun isSpeakerphoneOn(flag: Boolean, context: Context) {
        LogUtil.d(TAG, "isSpeakerphoneOn: $flag")
        val audioManager = context.getSystemService(AUDIO_SERVICE) as AudioManager
        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        // 每次接听的时候，默认听筒
        audioManager.isSpeakerphoneOn = flag
    }

    /**
     * 是否静音
     * @param flag Boolean
     * @param context Context
     */
    fun isMicrophoneMute(flag: Boolean, context: Context) {
        LogUtil.d(TAG, "isMicrophoneMute: $flag")
        val audioManager = context.getSystemService(AUDIO_SERVICE) as AudioManager
        audioManager.isMicrophoneMute = flag
    }


    /**
     * 播放assets的文件
     * @param context Context
     * @param streamType Int
     * @param assetsPath String
     * @param looping Boolean?
     */
    fun playAssetsSound(context: Context, streamType: Int, assetsPath: String, looping: Boolean?) {
        var context = context
        LogUtil.d(
            TAG,
            "playSound: assetsPath = $assetsPath and streamType = $streamType"
        )
        try {
            //每次播放的时候，先设置外放
            //isSpeakerphoneOn(flag = true, context)
            releaseMedia()
            val afd = context.assets.openFd(assetsPath)
            mMediaPlayer = MediaPlayer()
            mMediaPlayer!!.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length)
            mMediaPlayer!!.isLooping = looping!! //循环播放
            mMediaPlayer!!.setAudioStreamType(streamType)
            mMediaPlayer!!.prepare()
            mMediaPlayer!!.start()
            mMediaPlayer!!.setOnCompletionListener {
                LogUtil.d(TAG, "playSound onCompletion")
                mMediaPlayer!!.reset()
                mMediaPlayer!!.release()
                mMediaPlayer = null
            }
        } catch (e: Exception) {
            LogUtil.d(TAG, "playSound: " + Log.getStackTraceString(e))
        }
    }

    fun releaseMedia() {
        LogUtil.d(TAG, "releaseMedia")
        if (mMediaPlayer != null) {
            if (mMediaPlayer!!.isPlaying) {
                mMediaPlayer!!.stop()
            }
            mMediaPlayer!!.reset()
            mMediaPlayer!!.release()
            mMediaPlayer = null
        }
    }


}